package structure.string;

/**
* @description: kmp算法
* @author: wangjun
* @create: 2022-04-11
*/
public class KmpString {

    public static void main(String[] args) {
        System.out.println(kmp("jasvajava", "java"));
    }

    public static Integer kmp(String origin, String target){
        if(origin.length() == 0 || target.length() == 0){
            return -1;
        }
        char[] originArray = origin.toCharArray();
        char[] targetArray = target.toCharArray();
        int count = 0;
        for (int i = 0; i < originArray.length;i++) {
            int temp = i;
            for (int j = 0;j < targetArray.length; j++) {
                System.out.println("原串，第" + i + "轮，第" + j + "次" +originArray[temp]);
                System.out.println("子串，第" + i + "轮，第" + j + "次" +targetArray[j]);
                if (originArray[temp] == targetArray[j]) {
                    temp++;
                    count++;
                }else{
                    count = 0;
                    break;
                }
            }
            if(count == targetArray.length){
                return i;
            }
        }
        return -1;
    }
    
}